package com.rizhov.iterators;

import com.rizhov.graph.Graph;
import com.rizhov.graph.GraphEdge;
import com.rizhov.gui.MainWindow;

public class GraphEdgeIterator implements GraphIterator {

    /** singleton instance of Graph **/
    private Graph graph = Graph.getInstance();

    /** current edge start index **/
    private int currentStart = 0;

    /** current edge end index **/
    private int currentEnd = 0;

    /** indicator state **/
    private boolean isGoneOut = false;

    @Override
    public void next() {
	MainWindow.iteratorStartIndex = -1;
	MainWindow.iteratorEndIndex = -1;
	while (!(currentStart >= graph.getVertexCount() - 1 && currentEnd >= graph.getVertexCount() - 1)) {
	    isGoneOut = true;
	    if (currentEnd == graph.getVertexCount() - 1) {
		currentStart++;
		currentEnd = 0;
	    } else {
		currentEnd++;
	    }
	    if (graph.isEdgeExist(currentStart, currentEnd)) {
		MainWindow.iteratorStartIndex = currentStart;
		MainWindow.iteratorEndIndex = currentEnd;
		isGoneOut = false;
		return;
	    }
	}
    }

    @Override
    public void setToBegin() {
	MainWindow.iteratorStartIndex = -1;
	MainWindow.iteratorEndIndex = -1;
	currentStart = 0;
	currentEnd = 0;
	for (int i = 0; i < graph.getVertexCount(); i++, currentStart = i) {
	    for (int j = 0; j < graph.getVertexCount(); j++) {
		isGoneOut = true;
		currentStart = i;
		currentEnd = j;
		if (graph.isEdgeExist(i, j)) {
		    MainWindow.iteratorStartIndex = currentStart;
		    MainWindow.iteratorEndIndex = currentEnd;
		    isGoneOut = false;
		    return;
		}
	    }
	}
    }

    @Override
    public void setToEnd() {
	MainWindow.iteratorStartIndex = -1;
	MainWindow.iteratorEndIndex = -1;
	currentStart = graph.getVertexCount() - 1;
	currentEnd = graph.getVertexCount() - 1;
	for (int i = graph.getVertexCount() - 1; i >= 0; i--) {
	    for (int j = graph.getVertexCount() - 1; j >= 0; j--) {
		isGoneOut = true;
		currentStart = i;
		currentEnd = j;
		if (graph.isEdgeExist(currentStart, currentEnd)) {
		    MainWindow.iteratorStartIndex = currentStart;
		    MainWindow.iteratorEndIndex = currentEnd;
		    isGoneOut = false;
		    return;
		}
	    }
	}
    }

    @Override
    public void setData(Object newData) {
	if (!isGoneOut) {
	    String[] strArr = (String[]) newData;
	    if (graph.isEdgeExist(currentStart, currentEnd)) {
		graph.setEdgeWeight(currentStart, currentEnd, strArr[0]);
		graph.setEdgeData(currentStart, currentEnd, strArr[1]);
	    }
	}
    }

    @Override
    public Object getData() {
	if (!isGoneOut) {
	    String[] arr = new String[2];
	    arr[0] = graph.getEdgeWeight(currentStart, currentEnd).toString();
	    arr[1] = graph.getEdgeData(currentStart, currentEnd).toString();
	    return arr;
	} else {
	    return null;
	}
    }

    @Override
    public GraphEdge getEdge() {
	if (!isGoneOut) {
	    GraphEdge edge = new GraphEdge();
	    edge.edgeData = ((String[]) getData())[0];
	    edge.edgeWeight = ((String[]) getData())[1];
	    edge.startVertexIndex = currentStart;
	    edge.endVertexIndex = currentEnd;
	    return edge;
	} else {
	    return null;
	}
    }
}
